package com.bookstore.controller;

import com.bookstore.entity.User;
import com.bookstore.entity.UserMessage;
import com.bookstore.service.UserMessageService;
import com.bookstore.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

/**
 * @Author Cofe
 * @Date 2022/4/21 18:19
 */
@Controller
@RequestMapping("/usermessage")
public class UserMessageController {
    @Resource
    private UserServiceImpl us;
    @Resource
    private UserMessageService ums;

    @RequestMapping(
            value = "/findbyname",
            method = RequestMethod.POST
    )
    @CrossOrigin
    @ResponseBody
    //查找个人信息
    public UserMessage findByUsername(HttpSession httpSession)  {
        UserMessage usermessage=ums.findByUsername(httpSession.getAttribute("username")+"");
        //UserMessage usermessage = ums.findByUsername(username);
        return usermessage;
    }

    @RequestMapping(
            value = "/updateuser",
            method = RequestMethod.POST
    )
    @CrossOrigin
    @ResponseBody
    //修改用户信息
    public String updateuser(String name,String address,String gender,int age,String mail,String phone,String password,HttpSession httpSession)  {
        System.out.println(password);
        String username=httpSession.getAttribute("username")+"";
        if (!password.equals("")) {
            Integer integer = us.updatePass(username, password);
            System.out.println(integer);
            if(integer<=0){
                return "系统错误,请稍后重试...";
            }
        }
        String s = ums.updateuser(username, name, address, gender, age, mail, phone);
        return s;
    }
}
